var _ = require('lodash'),
    Q = require('q'),
    PublicEnum = require('../../../Base/server/controllers/Enum'),
    wareHouseController = require('../../../CBO/server/controllers/WareHouse.server.controller'),
    deliveryRecordController = require('../../../ProduceWork/server/controllers/DeliveryRecord.server.controller');

exports.getWareHouses = function (req, res) {
    wareHouseController.getAll().then(function (records) {
        res.json({
            Data: records,
            Error: null
        });
    }, function (err) {
        res.json({
            Data: null,
            Error: err
        });
    });
};

exports.getDeliveryRecords = function (req, res) {
    var wareHouse = req.body.WareHouse;
    deliveryRecordController.queryDeliveryRecords(wareHouse).then(function (records) {
        res.json({
            Data: records,
            Error: null
        });
    }, function (err) {
        res.json({
            Data: null,
            Error: err
        });
    });
};

exports.updateDeliveryStatus = function (req, res) {
    var deliveryIds = req.body.DeliveryIds,
        userId = req.body.UserId,
        deliveryRecords = [];
    _.forEach(deliveryIds, function (deliveryId) {
        deliveryRecords.push({
            _id: deliveryId,
            Status: 1,
            RowStatus: PublicEnum.RowStatus.Modify,
            CreatedBy: userId
        });
    });
    deliveryRecordController.update(deliveryRecords).then(function () {
        res.json({
            Data: true,
            Error: null
        });
    }, function (err) {
        res.json({
            Data: null,
            Error: err
        });
    });
};